﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace rtw
{
    public class entry
    {
        public string twitid;
        public string published;
        public string title;
        public string title_lowered;
        public string author;
        public int rank;
        public void convertPublishedToTime()
        {
            DateTime dt1 = DateTime.Parse(published);
            month = dt1.Month;
            day = dt1.Day;
            hour = dt1.Hour;
        }
        public int month,day,hour;

    }

    public class entrywriter
    {
        public static void write_entries_onefile(string folder, bool createNew, List<entry> e1)
        {
            string filename = folder + ".txt";
            TextWriter tw = getFile(createNew, filename);

            foreach (var e in e1)
            {
       
                tw.WriteLine(makerec(e));
            }

            foreach (var f in files)
            {
                f.Value.Dispose();
            }
            files.Clear();
        }
       public  static void write_entries(string folder,bool createNew, List<entry> e1)
        {
        
            foreach (var e in e1)
            {
                write_entry(folder, createNew,e);
            }

            foreach (var f in files)
            {
                f.Value.Dispose();
            }
            files.Clear();
        }
         static string norm(string val)
         {
             string res = val;
             res = res.Replace(',', '_');
             res = res.Replace('\r', '_');
             res = res.Replace('\n', '_');
             return res;
         }
        public static void set_data_folder(string name)
        {
            data_folder=name;
        }
        static string data_folder;
        static void write_entry(string folder,bool createNew,entry e)
        {
            
            DateTime dt = DateTime.Parse(e.published);

            string sdate = string.Format("{0:00}", dt.Month) + string.Format("{0:00}", dt.Day);
            string stime = string.Format("{0:00}", dt.Hour);
           // string filename = folder+"_"+sdate + stime + ".txt";
            string filename = folder + "_" + sdate + " all day " + ".txt";

           filename= System.IO.Path.Combine("../Data/"+data_folder, filename);
            getFile(createNew, filename).WriteLine(makerec(e));
        }
        static string makerec(entry e)
        {
            string rec = norm(e.published) + ", " + norm(e.author) + ", ";
            rec += norm(e.twitid) + ", " + norm(e.title);
            return rec;
        }

        static TextWriter getFile(bool createNew, string filename)
        {
            if (files.ContainsKey(filename))
            {
                return files[filename];
            }

            if (createNew)
                System.IO.File.Delete(filename);

            TextWriter streamWriter = new StreamWriter(filename, true, Encoding.UTF8);

            files.Add(filename, streamWriter);
            return streamWriter;
        }

        static Dictionary<string, TextWriter> files = new Dictionary<string, TextWriter>();
    }
}